package com.vlong.town.controller.admin.banner;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.banner.BannerInfo;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.service.banner.BannerInfoService;
import com.vlong.town.util.common.TownConstant;
import com.vlong.town.util.data.DataUtil;
import com.vlong.town.util.pagination.PageTag;

/**
 * Banner图信息Controller(便民平台)
 * @author Assassin
 */
@Controller
@RequestMapping("/bannerTourist")
public class TouristBannerInfoController extends BaseController {

    // Fields

    @Autowired
    private BannerInfoService bannerInfoService;// Banner图信息业务实现类

    /**
     * 进入Banner图信息列表页面
     * @return banner/list_banner
     */
    @RequestMapping("/list")
    public String gotoBannerInfoPage(
            @RequestParam(required=false, value="pageNo") Integer pageNo,
            @RequestParam(required=false, value="pageSize") Integer pageSize,
            @RequestParam(required=false, value="type") String type,
            @RequestParam(required=false, value="title") String title,
            @RequestParam(required=false, value="content") String content) {
        Integer t = null;
        if(type != null && !"-1".equals(type)) {
            t = Integer.parseInt(type);
        }
        PageHelper.startPage(getPageNo(pageNo), getPageSize(pageSize));
        List<BannerInfo> bannerInfoList = this.bannerInfoService.queryTouristList(t, title, content);
        PageInfo<BannerInfo> pageInfo = new PageInfo<BannerInfo>(bannerInfoList);
        PageTag<BannerInfo> pt = new PageTag<BannerInfo>(request, "list", pageInfo);
        request.setAttribute("pageHtml", pt.getPageHtml());
        request.setAttribute("type", type);
        request.setAttribute("title", title);
        request.setAttribute("content", content);
        for(BannerInfo bannerInfo : bannerInfoList) {
            bannerInfo.setTitle(DataUtil.delHTMLTag(bannerInfo.getTitle(), 10));
            bannerInfo.setContent(DataUtil.delHTMLTag(bannerInfo.getContent(), 35));
        }
        request.setAttribute("bannerInfoList", bannerInfoList);
        return "admin/bannerTourist/list_banner";
    }

    /**
     * 进入首页轮播Banner图信息添加页面
     * @return banner/add_banner
     */
    @GetMapping("/addLBanner")
    public String gotoLBannerInofAddPage() {
        return "admin/bannerTourist/add_lbanner";
    }

    /**
     * 进入体验旅游内容Banner图信息添加页面
     * @return banner/add_banner
     */
    @GetMapping("/addCBanner")
    public String gotoCBannerInofAddPage() {
        return "admin/bannerTourist/add_cbanner";
    }
    
    /**
     * 进入本地特产内容Banner图信息添加页面
     * @return banner/add_banner
     */
    @GetMapping("/addNBanner")
    public String gotoNBannerInofAddPage() {
        return "admin/bannerTourist/add_nbanner";
    }

    /**
     * 进入首页轮播Banner图信息修改页面
     * @return banner/edit_lbanner
     */
    @GetMapping("/editLBanner/{bannerId}")
    public String gotoLBannerInofEditPage(@PathVariable(required=true, value="bannerId") Long bannerId) {
        BannerInfo bannerInfo = this.bannerInfoService.queryById(bannerId);
        request.setAttribute("bannerInfo", bannerInfo);
        request.setAttribute("url",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + bannerInfo.getUrl());
        return "admin/bannerTourist/edit_lbanner";
    }

    /**
     * 进入首页内容Banner图信息修改页面
     * @return banner/edit_cbanner
     */
    @GetMapping("/editCBanner/{bannerId}")
    public String gotoCBannerInofEditPage(@PathVariable(required=true, value="bannerId") Long bannerId) {
        BannerInfo bannerInfo = this.bannerInfoService.queryById(bannerId);
        request.setAttribute("bannerInfo", bannerInfo);
        request.setAttribute("url",  request.getScheme()+"://"+request.getServerName()+":"+ request.getServerPort() + request.getContextPath() + "/cdkeditor/" + bannerInfo.getUrl());
        return "admin/bannerTourist/edit_cbanner";
    }

    /**
     * 添加Banner图信息
     * @param type Banner图类型
     * @param title Banner图标题
     * @param content Banner图内容
     * @param picture 缩略图
     * @param link 链接地址
     * @param color 标题颜色
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeAddBanner")
    public DataMessage addBanner(
            @RequestParam(name="type", required=true) String type,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="link", required=false) String link,
            @RequestParam(name="color", required=false) String color) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("Banner图标题不合法");
            return dm;
        }
        this.bannerInfoService.add(1L, Integer.parseInt(type), title, content, picture, link, color);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("Banner图添加成功");
        return dm;
    }

    /**
     * 修改Banner图信息
     * @param title Banner图标题
     * @param content Banner图内容
     * @param picture 缩略图
     * @param link 链接地址
     * @param color 标题颜色
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("executeEditBanner")
    public DataMessage editBanner(
            @RequestParam(name="bannerId", required=true) Long bannerId,
            @RequestParam(name="title", required=true) String title,
            @RequestParam(name="content", required=true) String content,
            @RequestParam(name="picture", required=true) String picture,
            @RequestParam(name="link", required=false) String link,
            @RequestParam(name="color", required=false) String color) {
        DataMessage dm = new DataMessage();
        // 后台验证
        if(!this.validateStringProperty(title, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("Banner图标题不合法");
            return dm;
        }
        this.bannerInfoService.update(bannerId, title, content, picture, link, color);
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
        dm.setMessage("Banner图修改成功");
        return dm;
    }

    /**
     * 删除Banner图
     * @param bannerId 主键值
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/delete")
    public DataMessage deleteUser(@RequestParam(required=true, value="bannerId") Long bannerId) {
        DataMessage dm = new DataMessage();
        if(bannerId != null && bannerId > 0L) {
            this.bannerInfoService.delete(bannerId);
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("Banner图删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

    /**
     * 批量删除Banner图
     * @param bannerIds 主键值集合字符串
     * @return DataMessage->Json
     */
    @ResponseBody
    @PostMapping("/batchDelete")
    public DataMessage batchDeleteUser(@RequestParam(required=true, value="bannerIds") String bannerIds) {
        DataMessage dm = new DataMessage();
        if(this.validateStringProperty(bannerIds, 1, 255)) {
            String[] bannerIdArray = bannerIds.split(",");
            for(String bannerId : bannerIdArray) {
                this.bannerInfoService.delete(Long.parseLong(bannerId));
            }
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("Banner图删除成功");
        } else {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("参数错误");
        }
        return dm;
    }

}
